package com.fjwt.gz.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.fjwt.gz.core.exception.BizException;
import com.fjwt.gz.db.entity.AgentInfoEntity;
import com.fjwt.gz.db.entity.FansSourceEntity;
import com.fjwt.gz.db.vo.FansSourceAgentVO;
import com.fjwt.gz.service.mapper.AgentInfoMapper;
import com.fjwt.gz.service.mapper.FansSourceMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户来源表 服务实现类
 * </p>
 *
 * @author shiyonghao
 * @since 2025-04-09
 */
@Slf4j
@Service
public class FansSourceService extends ServiceImpl<FansSourceMapper, FansSourceEntity> {

    @Autowired
    private FansSourceMapper fansSourceMapper;
    @Autowired
    private AgentInfoMapper agentInfoMapper;

    /**
     * 查询用户来源是否存在
     * @param fansId
     * @return
     */
    public FansSourceEntity findFansSourceByFansId(Long fansId,String appId) {
        return fansSourceMapper.findFansSourceByFansId(fansId,appId);
    }

    /**
     * 用户绑定机构网点
     * @param agentNo
     * @param lng
     * @param lat
     * @return
     */
    public FansSourceAgentVO findFansBindAgent(String agentNo, String lng, String lat) {
        AgentInfoEntity agentInfo = agentInfoMapper.selectById(agentNo);
        log.info("用户绑定机构信息：{}", JSONObject.toJSONString(agentInfo));
        if(null == agentInfo){
            throw new BizException("机构信息不存在！");
        }
        FansSourceAgentVO agentVO = agentInfoMapper.fansBindAgent(agentNo,agentInfo.getLevel(),lng,lat);
        log.info("用户绑定机构网点信息：{}",JSONObject.toJSONString(agentVO));
        if(null == agentVO){
            throw new BizException("机构网点信息不存在！");
        }
        return agentVO;
//        FansSourceEntity dto = new FansSourceEntity();
//        dto.setAgentNo(agentVO.getAgentNo());
//        dto.setFansId(fansId);
//        dto.setInviterType(0);
//        dto.setCreatedAt(new Date());
//        return fansSourceMapper.insert(dto);
    }
}
